<?php
/*
 * @Descripttion: 留言模块
 * @Author: 夜雨
 * @Email: 448408740@qq.com
 * @Date: 2019-11-25 14:23:52
 * @LastEditTime: 2019-11-26 17:19:44
 */
namespace app\admin\controller;

use app\admin\model\Message as MessageModel;
use tool\Log;

class Message extends Base
{
    // 留言列表
    public function index()
    {
        if(request()->isAjax()) {

            $limit = input('param.limit');
            $dianhua = input('param.dianhua');
            $status = input('param.status');
            $where = [];
            if (!empty($dianhua)) {
                $where[] = ['dianhua', 'like', $dianhua . '%'];
            }
            if (!empty($status)) {
                $where[] = ['status', 'eq', $status];
            }

            $model = new MessageModel();
            $list = $model->getMessages($limit, $where);

            if(0 == $list['code']) {

                return json(['code' => 0, 'msg' => 'ok', 'count' => $list['data']->total(), 'data' => $list['data']->all()]);
            }

            return json(['code' => 0, 'msg' => 'ok', 'count' => 0, 'data' => []]);
        }

        return $this->fetch();
    }

    // 编辑留言
    public function edit()
    {
        if (request()->isAjax()) {
            
            $param = input('post.');

            if (empty($param['dianhua'])) {
                return reMsg(-1, '', '请输入电话');
            }

            $model = new MessageModel();
            $res = $model->editMessage($param);

            Log::write("编辑留言：" . $param['dianhua']);

            return json($res);
        }

        $id = input('param.id');

        $model = new MessageModel();

        $this->assign([
            'message' => $model->getMessageById($id)['data'],
        ]);

        return $this->fetch();
    }

    // 删除留言
    public function delete()
    {
        if (request()->isAjax()) {

            $id = input('param.id');

            $model = new MessageModel();
            $res = $model->delMessageById($id);

            Log::write("删除留言：" . $id);

            return json($res);
        }
    }

    // 审核留言
    public function status()
    {
        if (request()->isAjax()) {

            $id = input('param.id');

            $model = new MessageModel();
            $res = $model->statusMessageById($id);

            return json($res);
        }
    }

}